'''xpath_案例5爬取国外WallpaperAbyss网站壁纸

https://wall.alphacoders.com/finding_wallpapers.php?lang=Chinese
这也是一个国外的壁纸网站，绝大部分的壁纸都是超清、4K的，而且壁纸的质量都很高。
'''
import urllib.request

from lxml import etree

# url = 'https://pagead2.googlesyndication.com/getconfig/sodar?sv=200&tid=gpt&tv=m202305010101&st=env'
# url = 'https://wall.alphacoders.com/popular.php?lang=Chinese&quickload=1198&page=2'
url = 'https://wall.alphacoders.com/by_resolution.php?w=1920&h=1080&lang=Chinese&quickload=209797&page=1'

headers = {
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36',
    'cookie': '__gads=ID=b727b179b40acacc:T=1682067278:S=ALNI_MZJcCAEFFnVsZb7nV3pyFoaloXLXw; _ga=GA1.1.171966336.1682067257; _ga_HL65XQTV30=GS1.1.1682213969.2.1.1682216789.0.0.0; wa_session=9u1gigsorm21ndn51elu1o8er4; __gpi=UID=00000bfb280a04c3:T=1682067278:RT=1683182245:S=ALNI_MZcQ6dgl4TGO0L6begQjNzrh04DXQ; __cf_bm=1jePF.EdM6rgeUeqzW0QzsuuZW4njUt.LAB_15PgUNw-1683183487-0-AWY3O48pG8gBSN1e8qnY4vMS41NWv4oKt6x9N64qwJJZB90KGZZqZL99NobpvVeswYSvjSWpxYBnOrA05jTWB9x/gMlFmzZ/O5rqisHNmp+K4oTVuqPInH0zHkLW+jkWWTz8yThci+m81JWm82W5xwc='
}

request = urllib.request.Request(url=url, headers=headers)
response = urllib.request.urlopen(request)
content = response.read().decode('utf-8')
# print(content)

html = etree.HTML(content)
print(html)
img_name_list = html.xpath('//div[@class="boxgrid"]/a/@title')
img_url_list = html.xpath('//div[@class="boxgrid"]/a//img/@src')
print('数据条数，：', len(img_name_list), len(img_url_list))

for index in range(len(img_url_list)):
    print(img_name_list[index], img_url_list[index])
